﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.BLL.DicMgr;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicReviewList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = new TopicBl().GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["topicid"] = topic.ID;

                this.BindData();
            }
        }

        Dictionary<int, Idea> ideasDic = null;
        private void BindData()
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);

            List<Idea> ideas = new IdeaBl().GetIdeaForReview(topicID);
            ideasDic = new Dictionary<int, Idea>();
            foreach (Idea idea in ideas)
                ideasDic.Add(idea.ID, idea);

            GvList.DataSource = new ReviewBl().GetReview(topicID);
            GvList.DataBind();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);


                IdeaReview review = (IdeaReview)e.Row.DataItem;
                Idea idea = ideasDic[review.IdeaID];

                ((Literal)e.Row.FindControl("LtrStartTime")).Text = review.ReviewTime.Value.ToString("HH:mm");
                ((Literal)e.Row.FindControl("LtrTitle")).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("ideadetail", "id", idea.ID.ToString()), idea.Title);
                ((Literal)e.Row.FindControl("LtrOwner")).Text = idea.Owner;
                ((Literal)e.Row.FindControl("LtrType")).Text = IdeaTypeDicMgr.GetByCode(idea.Type).Title;

                if (review.ReviewStatus == IdeaReviewStatus.Waiting)
                    e.Row.CssClass += " waiting";
                else if (review.ReviewStatus == IdeaReviewStatus.OnGoing)
                    e.Row.CssClass += " ongoing";
                else if (review.ReviewStatus == IdeaReviewStatus.Finished)
                    e.Row.CssClass += " finished";
                else if (review.ReviewStatus == IdeaReviewStatus.Summary)
                    e.Row.CssClass += " finished";
            }
        }

        protected void BtnRefresh_Click(object sender, EventArgs e)
        {
            this.BindData();
        }
    }
}